﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SystemathicsSound.cs" company="">
//   
// </copyright>
// <summary>
//   The systemathics sound.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Systemathics.FrameWork.Properties;

namespace Systemathics.FrameWork
{
    public class SystemathicsSound
    {
        private const int SND_ASYNC = 0x0001;
        private const int SND_MEMORY = 0x0004;
        private readonly BackgroundWorker bkgw;
        private readonly Dictionary<SytemathicsSounds, byte[]> Sounds;

        public SystemathicsSound()
        {
            bkgw = new BackgroundWorker();
            bkgw.DoWork += DoWork;
            Sounds = new Dictionary<SytemathicsSounds, byte[]>();

            Sounds[SytemathicsSounds.ACK] = new byte[Resources._2_ALL_ACKS.Length];
            Sounds[SytemathicsSounds.ACK_CANCEL] = new byte[Resources._2_ALL_ACKS.Length];
            Sounds[SytemathicsSounds.ACK_MODIFY] = new byte[Resources._2_ALL_ACKS.Length];
            Sounds[SytemathicsSounds.FILLED] = new byte[Resources._2_FILLED.Length]; 
            Sounds[SytemathicsSounds.PARTIALLY_FILLED] = new byte[Resources._2_PARTIALLY_FILLED.Length];
            Sounds[SytemathicsSounds.REJECT] = new byte[Resources._2_ALL_REJECTIONS.Length];
            Sounds[SytemathicsSounds.REJECT_CANCEL] = new byte[Resources._2_ALL_REJECTIONS.Length];
            Sounds[SytemathicsSounds.REJECT_MODIFY] = new byte[Resources._2_ALL_REJECTIONS.Length];
            Sounds[SytemathicsSounds.CONNECTED] = new byte[Resources.PROVIDER_CONNECTED.Length];
            Sounds[SytemathicsSounds.DISCONNECTED] = new byte[Resources.PROVIDER_DISCONNECTED.Length];

            Resources._2_ALL_ACKS.Read(Sounds[SytemathicsSounds.ACK], 0, (int) Resources._2_ALL_ACKS.Length);
            Resources._2_ALL_ACKS.Read(Sounds[SytemathicsSounds.ACK_CANCEL], 0, (int) Resources._2_ALL_ACKS.Length);
            Resources._2_ALL_ACKS.Read(Sounds[SytemathicsSounds.ACK_MODIFY], 0, (int) Resources._2_ALL_ACKS.Length);
            Resources._2_PARTIALLY_FILLED.Read(Sounds[SytemathicsSounds.PARTIALLY_FILLED], 0, (int) Resources._2_PARTIALLY_FILLED.Length);
            Resources._2_FILLED.Read(Sounds[SytemathicsSounds.FILLED], 0, (int) Resources._2_FILLED.Length);
            Resources._2_ALL_REJECTIONS.Read(Sounds[SytemathicsSounds.REJECT], 0, (int) Resources._2_ALL_REJECTIONS.Length);
            Resources._2_ALL_REJECTIONS.Read(Sounds[SytemathicsSounds.REJECT_CANCEL], 0, (int) Resources._2_ALL_REJECTIONS.Length);
            Resources._2_ALL_REJECTIONS.Read(Sounds[SytemathicsSounds.REJECT_MODIFY], 0, (int) Resources._2_ALL_REJECTIONS.Length);
            Resources.PROVIDER_CONNECTED.Read(Sounds[SytemathicsSounds.CONNECTED], 0, (int) Resources.PROVIDER_CONNECTED.Length);
            Resources.PROVIDER_DISCONNECTED.Read(Sounds[SytemathicsSounds.DISCONNECTED], 0, (int) Resources.PROVIDER_DISCONNECTED.Length);
        }

        public void CONNECTED()
        {
            if (!bkgw.IsBusy)
            bkgw.RunWorkerAsync(SytemathicsSounds.CONNECTED);
            // SystemSounds.Question.Play();
        }
        public void DISCONNECTED()
        {
            if (!bkgw.IsBusy)
            bkgw.RunWorkerAsync(SytemathicsSounds.DISCONNECTED);
            // SystemSounds.Question.Play();
        }
        public void REJECT_MODIFY()
        {
            if (!bkgw.IsBusy)
            bkgw.RunWorkerAsync(SytemathicsSounds.REJECT_MODIFY);
            // SystemSounds.Exclamation.Play();
        }
        public void REJECT_CANCEL()
        {
            if (!bkgw.IsBusy)
            bkgw.RunWorkerAsync(SytemathicsSounds.REJECT_CANCEL);
            // SystemSounds.Exclamation.Play();
        }
        public void REJECT()
        {
            if (!bkgw.IsBusy)
            bkgw.RunWorkerAsync(SytemathicsSounds.REJECT);
            // SystemSounds.Exclamation.Play();
        }
        public void PARTIALLY_FILLED()
        {
            if (!bkgw.IsBusy)
            bkgw.RunWorkerAsync(SytemathicsSounds.PARTIALLY_FILLED);
            // SystemSounds.Asterisk.Play();
        }
        public void FILLED()
        {
            if (!bkgw.IsBusy)
            bkgw.RunWorkerAsync(SytemathicsSounds.FILLED);
            // SystemSounds.Beep.Play();
        }
        public void ACK_MODIFY()
        {
            if (!bkgw.IsBusy)
            bkgw.RunWorkerAsync(SytemathicsSounds.ACK_MODIFY);
            // SystemSounds.Beep.Play();
        }
        public void ACK_CANCEL()
        {
            if (!bkgw.IsBusy)
            bkgw.RunWorkerAsync(SytemathicsSounds.ACK_CANCEL);
        }
        public void ACK()
        {
            if (!bkgw.IsBusy)
            bkgw.RunWorkerAsync(SytemathicsSounds.ACK);
            // SystemSounds.Beep.Play();
        }

        [DllImport("WinMM.dll")]
        private static extern bool PlaySound(byte[] szSound, IntPtr hMod, int flags);

        private void DoWork(object sender, DoWorkEventArgs e)
        {
            PlaySound(Sounds[(SytemathicsSounds)e.Argument], IntPtr.Zero, SND_ASYNC | SND_MEMORY);
        }
        //private void Play(SytemathicsSounds s)
        //{
        //    PlaySound(Sounds[s], IntPtr.Zero, SND_ASYNC | SND_MEMORY);
        //}
    }
}